﻿using System;
using System.Text;
using MyAnt.Core.World;

namespace MyAnt.Core.Visualization.World
{
	public class SectorThreeJsVisualizer : ISectorVisualizer
	{
		public object Visualize(Sector sector)
		{
			var sb = new StringBuilder();
			sb.Append("var sectorData = [" + Environment.NewLine);
			var first = true;
			for (var z = 0; z < sector.Height; z++)
			{
				sb.Append(Environment.NewLine + "\t\t");
				for (var y = 0; y < sector.Depth; y++)
				{
					for (var x = 0; x < sector.Width; x++)
					{
						var r = sector.Get(x, y, z);
						var m = 0;
						switch (r)
						{
							case Resource.ResourceType.Air: m = 0; break;
							case Resource.ResourceType.Food: m = 1; break;
							case Resource.ResourceType.Grass: m = 2; break;
							case Resource.ResourceType.Rock: m = 3; break;
							case Resource.ResourceType.Sand: m = 4; break;
							case Resource.ResourceType.Waste: m = 5; break;
							case Resource.ResourceType.Water: m = 6; break;
						}
						sb.Append((first ? " " : ",") + m.ToString("D2"));
						first = false;
					}
					sb.Append(Environment.NewLine + "\t\t");
				}
				sb.Append(Environment.NewLine);
			}
			sb.Append("\t];" + Environment.NewLine);

			var script =
@"
function initSector(mergedMeshGeometry) {
	var cubeGeometry = new THREE.CubeGeometry(20, 20, 20);
	var materialAir = new THREE.MeshBasicMaterial({ color: '0xccccff', opacity: 0.2, shading: THREE.FlatShading, overdraw: true });
	var materialFood = new THREE.MeshBasicMaterial({ color: '0xff6633', opacity: 0.5, shading: THREE.FlatShading, overdraw: true });
	var materialGrass = new THREE.MeshBasicMaterial({ color: '0x004400', opacity: 0.5, shading: THREE.FlatShading, overdraw: true });
	var materialRock = new THREE.MeshBasicMaterial({ color: '0x777777', opacity: 0.8, shading: THREE.FlatShading, overdraw: true });
	var materialSand = new THREE.MeshBasicMaterial({ color: '0xffdd00', opacity: 0.5, shading: THREE.FlatShading, overdraw: true });
	var materialWaste = new THREE.MeshBasicMaterial({ color: '0xff0000', opacity: 0.5, shading: THREE.FlatShading, overdraw: true });
	var materialWater = new THREE.MeshBasicMaterial({ color: '0x000055', opacity: 0.5, shading: THREE.FlatShading, overdraw: true });
	var materials = [materialAir, materialFood, materialGrass, materialRock, materialSand, materialWaste, materialWater];
	" + sb + @"
	var i = 0;
	for (var z = 0; z < " + sector.Depth + @"; z++) {
		for (var y = 0; y < " + sector.Height + @"; y++) { 
			for (var x = 0; x < " + sector.Width + @"; x++) { 
				var mesh = new THREE.Mesh(cubeGeometry, materials[sectorData[i]]);
				mesh.position.x = x * 50;
				mesh.position.y = z * 50;
				mesh.position.z = y * 50;
				mesh.overdraw = true;
				scene.add(mesh);
				//THREE.GeometryUtils.merge(mergedMeshGeometry, mesh);
				i++;
			}
		}
	}
	/*mergedMeshGeometry.computeFaceNormals();
	var group = new THREE.Mesh( mergedMeshGeometry, new THREE.MeshFaceMaterial() );
	group.matrixAutoUpdate = false;
	group.updateMatrix();
	scene.add( group );*/
}
";

			return script;
		}
	}
}
